package com.qk.controller;

import com.qk.common.Result;
import com.qk.entity.User;
import com.qk.service.UserService;
import com.qk.vo.LoginResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * ClassName: LoginController
 * Package: com.qk.controller
 * Description:
 *
 * @Author zt
 * @Create 2025/7/31 9:48
 * @Version 1.0
 */
@Slf4j
@RestController
public class LoginController {
    @Autowired
    private UserService userService;

    /*
    登录
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user) { //前端传递的数据是json格式，所以要用@RequestBody
        log.info("用户登录：{}", user);              //用户名和密码封装到user对象中

        //调用service，封装，返回结果
        LoginResultVo loginResultVo = userService.login(user.getUsername(), user.getPassword());
        if (loginResultVo == null) {
            return Result.error("登录失败");
        } else {
            return Result.success(loginResultVo);
        }
    }
}
